/**
 * 
 */
package com.airnewslinkosky.ticket.ui.financial;

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.airnewslinkosky.ticket.R;
import com.airnewslinkosky.ticket.model.BaseResult;
import com.airnewslinkosky.ticket.net.WebAccessor;
import com.airnewslinkosky.ticket.ui.BaseActivity;
import com.airnewslinkosky.ticket.util.AddrUtil;
import com.airnewslinkosky.ticket.util.Constants;
import com.airnewslinkosky.ticket.util.FlyUtil;
import com.mobclick.android.MobclickAgent;

/**
 * @author CX
 *
 */
public class FinancialAccountInput extends BaseActivity {
	
	private final static int SAVE_SUCCESS = 0;
	private final static int TIME_DIALOG = 1;
	public final int NORESULT = 3;
	public final int NOORDER = 4;
	public final int NOORDERSTAT = 5;
	public final int NET_ERROR=6;
	public final int SEARCH_ERROR=7;
	public  final int TIME_DIALOG_ID = 8;
	public  final int DIALOG_SINGLE_CHOICE = 9;
	public  final int DIALOG_LIST = 10;
	
	private TextView payAccount, payMethod;
	
	private Button backBut;
	
	String[] selectOptionList=null;
	String[] selectOptionValueList=null;
	
	private Calendar c = null;
	
	private ProgressDialog progressDialog = null;
	
	
	private AddrUtil addrUtil;
	
	private FlyUtil flyUtil;
	
    private SharedPreferences sp;
	private String loginNameRe = "";
	private String isLoninOK = null;//是否登录
	private String autoLogin = null;//记着登录
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.financial_accout_input);
		
		activityList.add(this);  
		
		addrUtil = new AddrUtil(this);
		flyUtil = new FlyUtil(this);
		
		payAccount = (TextView) findViewById(R.id.payAccount);
		payMethod = (TextView) findViewById(R.id.payMethod);
		
		findViewById(R.id.payMethod).setOnClickListener(clickListener);
		findViewById(R.id.saveAccountButton).setOnClickListener(clickListener);
		findViewById(R.id.clearAll).setOnClickListener(clickListener);
		findViewById(R.id.backBut).setOnClickListener(clickListener);
		
		sp = getSharedPreferences("UserInfo", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
		initConfig();
	}
	
	OnClickListener clickListener= new OnClickListener() { 
    	public void onClick(View v) { 
    		switch (v.getId()) {
    		case R.id.payMethod: {//订单状态
    			selectOptionList=new String[]{"支付宝"};
    			selectOptionValueList =  new String[]{""};
    			new AlertDialog.Builder(FinancialAccountInput.this)
    	            .setTitle("选择")
    	            .setItems(
    	            		selectOptionList,
    	            		new DialogInterface.OnClickListener() {
    	    	              public void onClick(DialogInterface dialog, int which) {                                      
    	    	            	  payMethod.setText(selectOptionList[which]);
    	    	            	  payMethod.setTag(selectOptionValueList[which]);
    	    	              }
    	    	            })
    	            .setNegativeButton(
    	            		"取消", 
    	            		new DialogInterface.OnClickListener() { 
    	    	              public void onClick(DialogInterface d, int which) { 
    	    	                d.dismiss(); 
    	    	              }
    	    	            })
    	            .show();
    	          break;
    		}    		
    		case R.id.searchButton: {//订单查询
    			saveAccount();
    	        break;
    		}
    		case R.id.clearAll: {
    			payMethod.setText("");
    			payAccount.setText("");
    			break;
    		}
    		case R.id.backBut:
    			Intent intentR = new Intent();
    			intentR.setClass(FinancialAccountInput.this, FinancialSearch.class);
    			startActivity(intentR);
				break;
    		}
    	}
	};
	
    //初始化配置
    private void initConfig() {
     sp = getSharedPreferences("UserInfo", 0);
     loginNameRe = sp.getString("loginName", "");
     isLoninOK = sp.getString("isLoninOK", null);
     autoLogin = sp.getString("isAutoLogin", null);
    }
    
    public Handler myHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case NORESULT:
				Toast.makeText(FinancialAccountInput.this, "未找到符合条件的帐单", Toast.LENGTH_LONG)
						.show();
				break;
			case NOORDER:
				Toast.makeText(FinancialAccountInput.this, "无帐单信息", Toast.LENGTH_LONG)
						.show();
				break;
			case NOORDERSTAT:
				Toast.makeText(FinancialAccountInput.this, "订单信息未获得更新", Toast.LENGTH_LONG)
						.show();
				break;
			case NET_ERROR:
				Toast.makeText(FinancialAccountInput.this, "网络超时，连接失败", Toast.LENGTH_LONG)
				.show();
				break;
			case SEARCH_ERROR:
				Toast.makeText(FinancialAccountInput.this, (String)msg.obj, Toast.LENGTH_LONG)
				.show();
				break;
			case SAVE_SUCCESS:
				Toast.makeText(FinancialAccountInput.this, (String)msg.obj, Toast.LENGTH_LONG)
				.show();
				break;
			}
			super.handleMessage(msg);
		}
	};
    
    private void saveAccount() {
    	
    	final String payAccountValue = payAccount.getText().toString();
    	final String payMethodValue = payMethod.getTag() != null ? payMethod.getTag().toString() : "";
		
		ContentValues values = new ContentValues();
		values.put("payAccount", payAccountValue);
		values.put("PayMethod", payMethodValue);
		progressDialog = ProgressDialog.show(this, "请稍等...", "保存支付帐号中...",true);
		final Thread thread1 = new Thread() {
			public void run() {
				
//				sp = getSharedPreferences("UserInfo", Context.MODE_WORLD_WRITEABLE | Context.MODE_WORLD_READABLE);
//		        String userIdLogin = sp.getString("userId", null);
//		        Log.i("FinancialSearch userIdLogin", userIdLogin);
//		        String userPwd =  sp.getString("userPwd", null);
//		        Log.i("FinancialSearch userPwd", userPwd);
		        
		        String userIdLogin = Constants.USERID;
		        String userPwd =  Constants.USERPWD;
		        String asToken =  Constants.ASToken;
		        
		        BaseResult result =  WebAccessor.saveFinancialAccount(FinancialAccountInput.this, payAccountValue, payMethodValue, userIdLogin, userPwd);
				
				if (progressDialog.isShowing()) {
					if (result== null) {
						Message msg = new Message();
						msg.what = NORESULT;
						myHandler.sendMessage(msg);
					}else if(!"0".equals(result.getRet_Code())){
						Message msg = new Message();
						msg.what = SEARCH_ERROR;
						msg.obj = result.getRet_Msg();
						myHandler.sendMessage(msg);
					}else{
						Message msg = new Message();
						msg.what = SAVE_SUCCESS;
						msg.obj = result.getRet_Msg();
						myHandler.sendMessage(msg);
					}
					progressDialog.dismiss();
				}
			}
		};
		progressDialog.setCancelable(true);
		thread1.start();
	}
	
	public void onResume() {     
		super.onResume();     
		MobclickAgent.onResume(this); 
	}  
	
	public void onPause() {     
		super.onPause();     
		MobclickAgent.onPause(this); 
	}  
}
